package org.hxyjs.dao.impl;

import org.hxyjs.dao.SalesagentDao;
import org.hxyjs.entity.Salesagent;
import org.hxyjs.mapper.SalesagentMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("salesagentDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class SalesagentDaoImpl implements SalesagentDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private SalesagentMapper salesagentMapper;
    public List<Salesagent>  getAllSalesagents() throws Exception{
        return salesagentMapper.selectByExample(null);
    }

    @Override
    public Integer deleteSalesagentBySano(String sano)  throws Exception{
        return salesagentMapper.deleteByPrimaryKey(sano);
    }

    @Override
    public Integer updateSalesagentBySano(Salesagent salesagent)  throws Exception{
        return salesagentMapper.updateByPrimaryKey(salesagent);
    }

    @Override
    public Integer addSalesagent(Salesagent salesagent)  throws Exception{
        salesagent.setSano(initId());
        return salesagentMapper.insertSelective(salesagent);
    }
    public String initId(){
        String maxSano = salesagentMapper.getMaxSano();
        return GenNumTools.initId("",3,maxSano);
    }
    @Override
    public List<Salesagent> getSalesagentByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)salesagentMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return salesagentMapper.getSalesagentByPage(map);
    }

    @Override
    public Salesagent getAccountByAccountName(String saname) {
        return salesagentMapper.getAccountByAccountName(saname);
    }


}
